//var  fs = require('fs');//自带的
//var promise = new Promise(function(resolve,reject){});
var request = require('request');

function diyi(wangzhi) {
  return new Promise(function (resolve,reject) {

    //fs.readFile(path, 'utf8', function (err, data) 
    request(wangzhi, function (err, response,body)
    {
      if(err){
        reject(err);   //失败
      }else{
        resolve(body);    //成功
      }
    })
  });
}

function dier(wangzhi,diyif) {
  return new Promise(function (resolve,reject) {

    //fs.readFile(path, 'utf8', function (err, data) 
    request({url:wangzhi,method:"POST",headers:{'Cookie': '_TESTCOOKIESUPPORT=1'},body:diyif}, function (err, response,body)
    {
      if(err){
        reject(err);   //失败
      }else{
        resolve(response);    //成功
      }
    })
  });
}


function disan(wangzhi,sid){
  return new Promise(function (resolve,reject) {

    //fs.readFile(path, 'utf8', function (err, data) 
    request({url:wangzhi,method:"GET",headers:{'Cookie': sid}}, function (err, response,body)
    {
      if(err){
        reject(err);   //失败
      }else{
        resolve(body);    //成功
      }
    })
  });
}







diyi('http://192.168.1.1')//这里不能加分号
.then(function(body){
      var regExp=/document.getElementById\("Frm_Logintoken"\).value = "(.*)";/;
    var regExp1=/document.getElementById\("Frm_Loginchecktoken"\).value = (\d+);/;
    var res = regExp.exec(body);
    var res1 = regExp1.exec(body);
    var hh="Frm_Logintoken=" +res[1] + "&Frm_Loginchecktoken=" + res1[1] + "&_cu_url=0&Right=2&Username=&UserRandomNum=95670920&Password=0b1c0803d1299bcc3f6d4bd997adef8c76a2f234f21fb9cb812d884f1568e88f&action=login";
	//console.log(body);
  //console.log(hh);

  return dier('http://192.168.1.1',hh);

},function(err){
	console.log("一层");

}


	)

.then(function(response){
  var sid=response.headers['set-cookie'][0].split(';')[0];
  //console.log(sid);
  return disan('http://192.168.1.1/getpage.gch?pid=1002&nextpage=status_lan_host_info_BJ_t.gch',sid)



},function(err){

  console.log("二层");
}
  )

//.then()

.then(function(body){


  var regExp3=/Transfer_meaning\('HostName\d+','(.*)'\);/g;
        var regExp4=/Transfer_meaning\('IPAddress\d+','(.*)'\);/g;
        var regExp5=/Transfer_meaning\('MACAddress\d+','(.*)'\);/g;
        var regExp6=/\\x\d[a-z]/g;
        var host='"';
        var ip='"';
        var mac='"';
        while(res=regExp3.exec(body)){

            //console.log(res[1]);
            var uu=res[1].replace(regExp6,'-');
            if (uu=='') {uu="None"}
            //console.log(uu);
            //host.push(uu);
            host=host+uu+'","';
        }

        //console.log(host);
        while(res=regExp4.exec(body)){
            //console.log(res[1]);
            var uu=res[1].replace(regExp6,'.');
            //console.log(uu);
            //ip.push(uu);

            ip=ip+uu+'","';
        }
        //console.log(ip);
        while(res=regExp5.exec(body)){
            //console.log(res[1]);
            var uu=res[1].replace(regExp6,':');
            //console.log(uu);
            //mac.push(uu);
            mac=mac+uu+'","';
        }
        //console.log(mac);

        //var fanhui={"host":host,"ip":ip,"mac":mac};
        //console.log(typeof fanhui);
        var fanhui='{"host":['+host.slice(0,-2)+'],"ip":['+ip.slice(0,-2)+'],"mac":['+mac.slice(0,-2)+']}';


        console.log(fanhui);
        //quanju=fanhui;
        //console.log(quanju);


},function(err){
  console.log("第三层");
}
  )

//console.log(fanhui);

